package com.dsteiner.xelavos.client.controller;

import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;

import com.dsteiner.widgets.client.JsUtil;
import com.dsteiner.widgets.client.Widgets.IHTML;
import com.dsteiner.widgets.client.Widgets.ILazyWidget;
import com.dsteiner.widgets.client.Widgets.IWidget;
import com.dsteiner.widgets.client.Widgets.WidgetListenerAdapter;
import com.dsteiner.widgets.client.controller.PopupOverlayController;
import com.dsteiner.widgets.client.event.EventManager;
import com.dsteiner.widgets.client.model.PopupOverlayModel;
import com.dsteiner.xelavos.client.constants.Constants;
import com.dsteiner.xelavos.client.domain.User;
import com.dsteiner.xelavos.client.model.AuthenticationModel;
import com.google.gwt.event.dom.client.ClickEvent;
import com.google.gwt.event.dom.client.ClickHandler;
import com.google.gwt.user.client.ui.RootPanel;

public class AuthenticationController {

	private AuthenticationModel authenticationModel;
	private IWidget loginWidget;
	private ILazyWidget createAccountWidget;
	private IHTML messageHtml;
	private PopupOverlayModel createAccountPopupModel;
	private IHTML createAccountLink;
	private ILazyWidget authenticationWidget;

	public void registerAuthenticationModel(AuthenticationModel authenticationModel) {
		this.authenticationModel = authenticationModel;
		authenticationModel.addPropertyChangeListener("user", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateUserLoggedIn();
				updateAuthenticationCookie();
			}
		});
		authenticationModel.addPropertyChangeListener("message", new PropertyChangeListener(){
			public void propertyChange(PropertyChangeEvent evt) {
				updateMessage();
			}
		});
	}

	private void updateAuthenticationCookie() {
		User user = authenticationModel.getUser();
		if(user == null){
			JsUtil.getInstance().removeCookie(Constants.COOKIE_NAME_USER_ID);
		}
		else{
			JsUtil.getInstance().addCookie(Constants.COOKIE_NAME_USER_ID, user.getId()+"");
		}
	}

	public void registerAuthenticationWidget(ILazyWidget authenticationWidget) {
		this.authenticationWidget = authenticationWidget;
		authenticationWidget.addWidgetListener(new WidgetListenerAdapter(){
			@Override
			public void onAttach(IWidget widget) {
				updateUserLoggedIn();
				updateMessage();
			}
		});
	}

	public void registerCreateAccountLink(final IHTML createAccountLink) {
		this.createAccountLink = createAccountLink;
		createAccountLink.addClickHandler(new ClickHandler(){
			public void onClick(ClickEvent event) {
				PopupOverlayController.showAtCenter(createAccountPopupModel);
			}
		});
	}

	public void registerCreateAccountWidget(ILazyWidget createAccountWidget) {
		this.createAccountWidget = createAccountWidget;
	}

	public void registerLoginWidget(IWidget loginWidget) {
		this.loginWidget = loginWidget;
	}

	public void registerMessageHtml(IHTML messageHtml) {
		this.messageHtml = messageHtml;
	}

	private void updateMessage() {
		String message = authenticationModel.getMessage();
		messageHtml.setVisible(message != null);
		messageHtml.setHTML(message);
	}

	private void updateUserLoggedIn() {
		User user = authenticationModel.getUser();
		loginWidget.setVisible(user == null);
		authenticationWidget.setVisible(user == null);
		RootPanel.get().setStyleName(user != null ? "logged-in" : "");
		if(user != null)
			createAccountPopupModel.setVisible(false);
	}

	public void registerCreateAccountPopupModel(PopupOverlayModel createAccountPopupModel) {
		this.createAccountPopupModel = createAccountPopupModel;
	}

	public void registerEventManager(EventManager eventManager) {
	}

}
